require(['./config'], function() {
    require(['jquery', 'template', 'loadHAF'], function($, template) {
        class Cart {
            constructor() {
                this.cart = JSON.parse(localStorage.cart) || []

                this.initCart()
                this.addListener()

            }
            initCart() {
                if (this.cart.length === 0) {
                    $('.body_not').show()
                        .next().hide()
                    return
                }
                $(".body_not").hide()
                    .next().removeClass("hidden")
                const html = template("cart-List", { cart: this.cart })
                $(".Cart_body").html(html)
            }
            addListener() {
                    $(".Cart_body").on("click", ".del", this.delete.bind(this)) //删除
                    $(".Cart_body").on("click", ".into, .pass", this.cartMath.bind(this)) // 数量加减
                    $(".Cart_body").on("blur", ".goodsNum", this.cartMath.bind(this))
                    $(".allgoods").on("click", this.allGoods.bind(this)) //选中
                    $(".Cart_body").on("click", ".checkedgoods", this.someGoods.bind(this))
                }
                //delete
            delete(e) {
                const _tr = $(e.target).parents("tr")
                const _id = _tr.data("id")
                this.cart = this.cart.filter(curr => curr.id != _id)
                localStorage.cart = JSON.stringify(this.cart)
                _tr.remove()
                this.allPrice()

            }
            cartMath(e) {
                const _tr = $(e.target).parents("tr")
                const _id = _tr.data("id")
                const prod = this.cart.find(curr => curr.id == _id)
                if ($(e.target).is(".into")) {
                    prod.amount += 1
                } else if ($(e.target).is(".pass")) {
                    if (prod.amount <= 1)
                        return
                    prod.amount -= 1

                } else if ($(e.target).is(".goodsNum")) {
                    const _num = $(e.target).val()
                    if (/^[1-9]\d*$/.test(_num)) {
                        $(e.target).val(prod.amount)
                        return
                    }
                    prod.amount = Number(_num)
                }
                localStorage.cart = JSON.stringify(this.cart)
                _tr.find(".goodsNum").val(prod.amount)
                _tr.find(".subprice").text((prod.amount * prod.price).toFixed(2))

                this.allPrice()

            }
            allGoods(e) {
                const _chacked = $(e.target).prop("checked")
                $(".checkedgoods,.allgoods").prop("checked", _chacked)
                this.allPrice()

            }
            someGoods(e) {
                $(".allgoods").prop("checked", $(".checkedgoods:checked").length === this.cart.length)
                this.allPrice()
            }
            allPrice() {
                let _Pri = 0
                $(".checkedgoods:checked").each((index, element) => {
                    _Pri += Number($(element).parents("tr").find(".subprice").text())
                })
                $(".allPri").text(_Pri.toFixed(2))
            }

        }

        new Cart()
    })
})